package com.lagou.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;

public class Config {
    private static String zkServerAddress = "127.0.0.1:2181";
    private static String jdbcPath = "/txy_3_2_jdbc";

    private static String[] jdbdConfigs = new String[] {"com.mysql.cj.jdbc.Driver#jdbc:mysql://localhost:3306/jpa#root#123456",
            "com.mysql.cj.jdbc.Driver#jdbc:mysql://127.0.0.1:3306/jpa#root#123456"};

    private static ZkClient zkClient;

    private static  ComboPooledDataSource comboPooledDataSource;

    public static ZkClient getZkClient() {
        if(zkClient==null) {
            initZkClient();
        }
        return zkClient;
    }

    public static ComboPooledDataSource getDataSource() {
        if(comboPooledDataSource==null) {
            initDataSource();
        }
        return comboPooledDataSource;
    }


    private static void initZkClient() {
        zkClient = new ZkClient(zkServerAddress);
        System.out.println("zkClient会话创建");

        if(!zkClient.exists(jdbcPath)) {
//   com.mysql.jdbc.Driver         com.mysql.cj.jdbc.Driver
            zkClient.create(jdbcPath, jdbdConfigs[0], CreateMode.PERSISTENT);
        }
    }
    private static boolean flag = true;

    private static void initDataSource() {
        try {
            if(comboPooledDataSource!=null ) {
                comboPooledDataSource.close();
                comboPooledDataSource = null;
            }
            comboPooledDataSource = new ComboPooledDataSource();
            String[] jdbcInfo = getZkClient().readData(jdbcPath).toString().split("#");
            comboPooledDataSource.setDriverClass(jdbcInfo[0]);
            comboPooledDataSource.setJdbcUrl(jdbcInfo[1]);
            comboPooledDataSource.setUser(jdbcInfo[2]);
            comboPooledDataSource.setPassword(jdbcInfo[3]);
            System.out.println("数据源创建成功");

            getZkClient().subscribeDataChanges(jdbcPath, new IZkDataListener() {
                public void handleDataChange(String s, Object o) throws Exception {
                    System.out.println("捕捉到节点变化事件，重新建立对数据的连接"+o);

                }

                @Override
                public void handleDataDeleted(String s) throws Exception {
                    System.out.println("捕捉到节点删除事件，关闭连接");
                    comboPooledDataSource.close();
                    comboPooledDataSource = null;
                }
            });
            if(flag) {
                flag=false;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for(int i=0;;i++) {
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println("修改jdbc连接");
                            getZkClient().writeData(jdbcPath, jdbdConfigs[i% jdbdConfigs.length]);
                        }
                    }
                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
